import unittest
from random import randint
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.select import Select
from HTMLTestRunner import HTMLTestRunner


class JXC_test_rep(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get('http://localhost/xiaoyulianxi/index.asp')
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()

    def tearDown(self):
        sleep(3)
        self.driver.quit()

    def test_login(self):
        self.assertIn('后台管理系统',self.driver.page_source)

    def test_del_unit(self):
        self.driver.switch_to.frame('left')
        self.driver.find_element_by_css_selector(
            '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)').click()
        sleep(1)
        self.driver.find_element_by_css_selector(
            '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)').click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(1)
        self.driver.find_element_by_name('chkall').click()
        sleep(1)
        self.driver.find_element_by_css_selector('input.button:nth-child(2)').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        self.assertNotIn("name='ID'", self.driver.page_source)

    def test_add_unit(self):
        self.driver.switch_to.frame('left')
        self.driver.find_element_by_css_selector(
            '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)').click()
        sleep(1)
        self.driver.find_element_by_css_selector(
            '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)').click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_class_name('body > table > tbody > tr:nth-child(1) > td:nth-child(2) > table > tbody > tr > td:nth-child(2) > input').click()
        sleep(1)
        self.driver.find_element_by_name('danwei').send_keys('KG')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        self.assertIn('KG', self.driver.page_source)

    def test_add_bigclass(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        sleep(1)
        self.driver.find_element_by_css_selector(
            'body > table > tbody > tr:nth-child(13) > td > img').click()
        self.driver.find_element_by_css_selector(
            '#g_6 > table > tbody > tr:nth-child(5) > td > table > tbody > tr > td:nth-child(2)').click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_css_selector('input.button:nth-child(1)').click()
        sleep(1)
        self.driver.find_element_by_name('bigclass').send_keys('生活用品')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()

        self.assertIn('生活用品', self.driver.page_source)

    def test_add_samllclass(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        sleep(1)
        self.driver.find_element_by_css_selector(
            'body > table > tbody > tr:nth-child(13) > td > img').click()
        self.driver.find_element_by_css_selector(
            '#g_6 > table > tbody > tr:nth-child(6) > td > table > tbody > tr > td:nth-child(2)').click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_css_selector('input.button:nth-child(1)').click()
        sleep(1)
        sel_list = Select(self.driver.find_element_by_css_selector(
            '.toptable > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > select:nth-child(1)'))
        len_sel = len(sel_list.options)
        rand = randint(0, len_sel - 1)
        sel_list.select_by_index(rand)
        sleep(1)
        self.driver.find_element_by_name('smallclass').send_keys('矿泉水')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        self.assertIn('矿泉水', self.driver.page_source)


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(JXC_test_rep('test_login'))
    suite.addTest(JXC_test_rep('test_del_unit'))
    suite.addTest(JXC_test_rep('test_add_unit'))
    suite.addTest(JXC_test_rep('test_add_bigclass'))
    suite.addTest(JXC_test_rep('test_add_samllclass'))
    return suite

with open('D:\pycharm\untitled\report.html','wb') as f:
    runner = HTMLTestRunner(stream=f,title='测试报告')
    runner.run(test_suite())